import models.db as db
from flask import request,  jsonify


# 获取权限列表
def role_view():
    sql = "select * from role"
    rows = db.get_db(sql)
    rows = [dict(row) for row in rows]
    return jsonify(rows)


# 添加角色
def role_add():
    message = request.get_json()
    message_data = []
    sum_data = []
    for i in message.values():
        message_data.append(i)
    sum_data.append(tuple(message_data))
    sql = "insert into role (role_name,roleDesc) values (?, ?)"
    db.query_db(sql, sum_data)
    return jsonify({'status': '200', 'msg': '添加角色成功！'})


# 打开编辑角色
def role_edit_open():
    message = request.get_json()
    print(message)
    num = message['role_id']
    sql = "select role_name,roleDesc from role where role_id = ?"
    rows = db.get_db(sql, (num,))
    rows = [dict(row) for row in rows]
    return jsonify(rows)


# 编辑角色
def role_edit():
    message = request.get_json()
    sql = "update role set roleDesc= ? where role_name= ?"
    db.get_db(sql, (message['roleDesc'], message['role_name']))
    return jsonify({})


# 删除角色
def role_drop():
    message = request.get_json()
    num = message['role_id']
    sql = "delete from role where role_id = ?"
    db.get_db(sql, (num,))
    return jsonify({})
